<template>
  <div>
    <!-- 分享 -->
    <t-modal
      title="分享"
      width="900px"
      :visible="visible"
      @cancel="closeModal()"
      wrapClassName="sharing-scene-container"
      forceRender
    >
      <div class="sharing-modal-body">
        <a-form-model
          ref="ruleForm"
          :model="form"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <a-form-model-item ref="validity" label="有效期" prop="validity">
            <a-radio-group v-model="form.validity">
              <a-radio :value="7">
                7天
              </a-radio>
              <a-radio :value="30">
                30天
              </a-radio>
              <a-radio :value="0">
                永久
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item ref="isValidate" label="提取码" prop="isValidate">
            <a-radio-group v-model="form.isValidate">
              <a-radio :value="1">
                有提取码
              </a-radio>
              <a-radio :value="0">
                无提取码
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item ref="desc" label="备注" prop="desc">
            <a-input v-model="form.desc" type="textarea" />
          </a-form-model-item>
        </a-form-model>
      </div>
      <template slot="footer">
        <a-button @click="closeModal()">取消</a-button>
        <a-button type="primary" :loading="loading" @click="createLink()">
          创建链接
        </a-button>
      </template>
    </t-modal>
    <!-- 链接弹窗 -->
    <t-modal
      title="分享链接"
      width="900px"
      :visible="linkVisible"
      @cancel="closeLinkModal()"
      wrapClassName="sharing-scene-link-container"
      forceRender
    >
      <div class="sharing-modal-body">
        <a-form-model
          ref="ruleForm"
          :model="form"
          :label-col="{ span: 3 }"
          :wrapper-col="{ span: 20 }"
        >
          <div class="state-tip">
            <a-icon type="check-circle" theme="filled" />成功创建私密链接！
          </div>
          <a-form-model-item ref="url" label="链接" prop="url">
            <a-input v-model="form.url" disabled></a-input>
          </a-form-model-item>
          <a-form-model-item
            ref="verificationCode"
            label="验证码"
            prop="verificationCode"
            v-if="form.verificationCode"
          >
            <a-input v-model="form.verificationCode" disabled></a-input>
          </a-form-model-item>
          <div class="link-tip">
            <template v-if="form.validity">
              链接将于 <span class="text">{{ form.validity }}</span>
              天后失效
            </template>
            <template v-else>链接永久有效</template>
          </div>
        </a-form-model>
      </div>
      <template slot="footer">
        <a-button @click="closeLinkModal()">取消</a-button>
        <a-button
          type="primary"
          class="copy-btn"
          data-clipboard-action="copy"
          :data-clipboard-text="form.url"
          @click="copyLinkUrl()"
        >
          复制链接
        </a-button>
      </template>
    </t-modal>
  </div>
</template>

<script>
import moment from "moment";
import Clipboard from "clipboard";
import { experienceSharSave } from "@/api/knowledgeBase";
export default {
  name: "shareModal",
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    uuid: {
      type: [Number, String],
      default: "",
    },
  },
  data() {
    return {
      loading: false,
      linkVisible: false,
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      other: "",
      form: {
        //有效期
        validity: 7,
        //是否需要验证
        isValidate: 1,
        //备注
        desc: "",
        //
        url: "",
        //验证码
        verificationCode: "",
      },
    };
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
        }
      },
      immediate: true,
    },
  },
  mounted() {},
  methods: {
    //创建链接
    createLink() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          experienceSharSave({
            fileUuid: this.uuid,
            expireDate: this.calcValidity(),
            isValidate: this.form.isValidate,
            remark: this.form.desc,
          })
            .then((res) => {
              if (res.statusCode == 300) {
                this.$message.error("链接生成失败，请稍后重试！");
                return;
              }
              this.closeModal(1);
              let domain = location.origin;
              this.form.verificationCode = res.verificationCode;
              this.form.url = `${domain}/#/knowledgeBase/shareExperienceView?uuid=${res.uuid}`;
              this.linkVisible = true;
            })
            .catch((_) => {})
            .finally((_) => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },

    //计算链接有效期
    calcValidity() {
      let { validity } = this.form;
      if (!validity) {
        return 0;
      } else {
        let today = moment();
        let futureDate = today.add(validity, "days");
        return futureDate.format("YYYY-MM-DD");
      }
    },

    //复制链接
    copyLinkUrl() {
      if (!this.form.url) {
        this.$message.warning("没有文本可复制");
        return;
      }
      let clipboard = new Clipboard(".copy-btn");
      clipboard.on("success", () => {
        this.$message.success("链接复制成功");
      });
      clipboard.on("error", () => {
        this.$message.error("链接复制失败，请手动复制！");
      });
      setTimeout(() => {
        // 销毁实例
        clipboard.destroy();
      }, 122);
    },

    //关闭
    closeModal(type = 0) {
      this.$emit("update:visible", false);
      if (!type) {
        this.$emit("close");
      }
    },

    closeLinkModal() {
      this.linkVisible = false;
      this.$emit("close");
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.sharing-scene-container {
  .sharing-modal-body {
    display: flex;
    flex-direction: column;
  }
}

/deep/.sharing-scene-link-container {
  .sharing-modal-body {
    .state-tip {
      display: flex;
      align-items: center;
      padding-left: 82px;
      margin-bottom: 20px;
      font-family: Microsoft YaHei, Microsoft YaHei;
      font-weight: 400;
      font-size: 14px;
      color: #239b53;
      .anticon-check-circle {
        margin-right: 6px;
        font-size: 22px;
      }
    }
    .link-tip {
      padding-left: 108px;

      margin-top: -4px;
      .text {
        color: #1677ff;
      }
    }
  }
}
</style>
